package com.ms.infrastructure.spring;

import com.google.common.collect.ImmutableMap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;

public class ParametroProperties extends Properties {
    // PARAMETROS
    int PROTOCOL_WS_SECURITY = 22;
    int SERVER_WS_SECURITY = 23;
    int PORT_WS_SECURITY = 24;
    int URL_WS_SECURITY = 25;
    public static final int OPERADOR_TRANSPORTISTA = 112;
    public static final int OPERADOR_REPRESENTANTE = 113;
    public static final int OPERADOR_DEPOSITO_ADUANERO = 114;
    public static final int OPERADOR_AGENTE_ADUANA = 115;
    public static final int OPERADOR_SADA = 116;
    public static final int OPERADOR_OPERADOR_PORTUARIO = 117;

    int URL_WS_SOAINT = 78;

    int PROTOCOL_API = 1;
    int SERVER_API = 2;
    int PORT_API = 3;
    int URL_API = 4;
    int OPERATOR_ID_API = 5;
    int COMPLEX_ID_API = 6;
    int FACILITY_ID_API = 7;
    int YARD_ID_API = 8;
    int USER_API = 9;
    int PASSWORD_API = 10;
    int AUDIT_USER = 11;

    int PROTOCOL_QUERY = 12;
    int SERVER_QUERY = 13;
    int PORT_QUERY = 14;
    int URL_QUERY = 15;
    int OPERATOR_ID_QUERY = 16;
    int COMPLEX_ID_QUERY = 17;
    int FACILITY_ID_QUERY = 18;
    int YARD_ID_QUERY = 19;
    int USER_QUERY = 20;
    int PASSWORD_QUERY = 21;

    int N4_DATE_FMT = 31;      // Normal Date
    int N4_UQ_DATE_FMT = 32;  // Universal query

    int FTP_COPARN_HOST = 34;
    int FTP_COPARN_USER = 35;
    int FTP_COPARN_PASS = 36;
    int FTP_COPARN_DIRECTORY = 37;
    int FTP_COPARN_FILENAME = 38;
    int FTP_PREADVISE_FILENAME = 39;

    int APP_EJEC_MODE_PROD_UAT = 40;// Valores posibles PROD/UAT

    int FTP_MANIFIESTO_HOST = 41;
    int FTP_MANIFIESTO_USER = 42;
    int FTP_MANIFIESTO_PASS = 43;
    int FTP_MANIFIESTO_DIRECTORY = 44;
    int FTP_MANIFIESTO_FILENAME = 45;


    int FTP_TDR_EDI_HLC_HOST = 87;       //TODO REUSAR NO SE USA
    int FTP_TDR_EDI_HLC_USER = 88;       //TODO REUSAR NO SE USA
    int FTP_TDR_EDI_HLC_PASS = 89;       //TODO REUSAR NO SE USA
    int FTP_TDR_EDI_HLC_DIRECTORY = 90;  //TODO REUSAR NO SE USA

    int SMTP_TO_TDR_EDI_HLC = 91;

    int REPORT_SERVER_URL = 95;        //http://10.208.101.181:20080/ws/


    int SMTP_HOST = 46;
    int SMTP_USER = 47;
    int SMTP_PASS = 48;
    int SMTP_FROM = 49;
    int SMTP_TO_PRECINTOS = 50;

    int POP3_HOST = 51;
    int POP3_MANIF_ADUANA_USR = 52;
    int POP3_MANIF_ADUANA_PWD = 53;

    int INTERCONEXION_BANCOS_URL = 54;

    int PROTOCOL_API_BILLING = 61;
    int SERVER_API_BILLING = 62;
    int PORT_API_BILLING = 63;
    int URL_API_BILLING = 64;
    int OPERATOR_ID_API_BILLING = 65;
    int COMPLEX_ID_API_BILLING = 66;
    int FACILITY_ID_API_BILLING = 67;
    int YARD_ID_API_BILLING = 68;
    int USER_API_BILLING = 69;
    int PASSWORD_API_BILLING = 70;

    int SMTP_TO_CAL_CDL = 71;
    int SOLICITUD_USUARIO_MAIL_TEST = 74;

    int FACTURACION_QUEUED_MAX_ROWS = 73; //200
    int FACTURACION_QUEUED_DAYS_OLD = 75; //30

    int SMTP_TO_TDR_FINAL = 76;
    int SMTP_TO_FACTURACION = 77;

    int SMTP_TO_SAFETY = 79;

    int AREA_TO_VISITA_ACCESO = 80;
    int USER_ENTREVISTADOR_TO_VISITA_ACCESO = 81;

    int SMTP_TO_RPT_EVT_FACTURABLES = 82;

    int POP3_ACCESOS_USR = 83;
    int POP3_ACCESOS_PWD = 84;

    int POP3_CHOFERES_USR = 85;
    int POP3_CHOFERES_PWD = 86;

    int NAUTILIUS_BUSCAR_ANTES = 92;
    int NAUTILIUS_BUSCAR_DESPUES = 93;

    int CDL_INDEX_SHEET = 0;
    int CDL_INDEX_INITIAL_ROW = 5;

    int FAS_MAIL_LIST = 96;
    int REJECT_INGATE_MAIL_LIST = 97;

    static final Map<String, String> EQUIVALENCIAS = ImmutableMap.<String, String>builder()
            .put("ws.dpwc.sunat", "102")
            .put("ws.dpwc.deposito", "103")
            .build();

    DataSource dataSource;

    public ParametroProperties(DataSource dataSource) {
        super();
        this.dataSource = dataSource;
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        List<Map<String, Object>> codeValueList = jdbcTemplate.queryForList("select parameter_code,parameter_value from dpwc_app.tbl_parameter");
        for (Map<String, Object> codeValue : codeValueList) {
            if (StringUtils.isBlank((String) codeValue.get("PARAMETER_VALUE"))) {
                continue;
            }
            setProperty((codeValue.get("PARAMETER_CODE")).toString(), (codeValue.get("PARAMETER_VALUE")).toString());
        }
        String urlWsDpwcSeguridad = getUrlWsDpwcSeguridad();
        setProperty("ws.dpwc.seguridad", urlWsDpwcSeguridad);
        setProperty("ws.dpwc.deposito", getProperty(EQUIVALENCIAS.get("ws.dpwc.deposito")));
        setProperty("ws.dpwc.sunat", getProperty(EQUIVALENCIAS.get("ws.dpwc.sunat")));
        // todo comentar
//        setProperty("ws.dpwc.deposito", "http://localhost:20080/DepositoTemporal/depositoTemporalPort");
//        setProperty("ws.dpwc.sunat", "http://localhost:20080/Sunat/sunatPort");

//        setProperty("ws.dpwc.deposito", "http://localhost:8780/DepositoTemporal/depositoTemporalPort");
//        setProperty("ws.dpwc.sunat", "http://localhost:8780/Sunat/sunatPort");

    }

    public final static List<Integer> PARAMETRO_CUSTOM_LIST= Arrays.asList(
            ParametroProperties.OPERADOR_AGENTE_ADUANA,
            ParametroProperties.OPERADOR_DEPOSITO_ADUANERO,
            ParametroProperties.OPERADOR_OPERADOR_PORTUARIO,
            ParametroProperties.OPERADOR_REPRESENTANTE,
            ParametroProperties.OPERADOR_SADA,
            ParametroProperties.OPERADOR_TRANSPORTISTA);

    private String getUrlWsDpwcSeguridad() {
        StringBuffer urlBuffer = new StringBuffer();
        urlBuffer.append(getProperty(PROTOCOL_WS_SECURITY));
        urlBuffer.append("://");
        urlBuffer.append(getProperty(SERVER_WS_SECURITY));
        urlBuffer.append(":");
        urlBuffer.append(getProperty(PORT_WS_SECURITY));
        urlBuffer.append(getProperty(URL_WS_SECURITY));
        return urlBuffer.toString().replaceAll("DpwcSecurityWebService", "DpwcSecurityWebService2");
    }

    public String getProperty(int intKey) {
//        if (intKey == 109 ) {
        if (intKey == 109 || PARAMETRO_CUSTOM_LIST.contains(intKey) ) {
            JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
            List<Map<String, Object>> codeValueList = jdbcTemplate.queryForList("select parameter_code,parameter_value from dpwc_app.tbl_parameter where parameter_code=" + intKey);
            for (Map<String, Object> codeValue : codeValueList) {
                setProperty((codeValue.get("PARAMETER_CODE")).toString(), codeValue.get("PARAMETER_VALUE")==null?"":(codeValue.get("PARAMETER_VALUE")).toString());
            }
        }
        return getProperty("" + intKey);
    }

    public Object setProperty(String key, String value) {
        return super.setProperty(key, value);
    }
}